package com.juzhencms.apps.common.aop;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juzhencms.apps.busi.service.CommonService;
import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.util.DataLogUtil;

import lombok.extern.slf4j.Slf4j;

@Aspect
@Slf4j
@Component
public class WebControllerAop {
	
	//过滤数据时所识别的list类型
	private static List listClassList;
	//过滤数据时所识别的map类型
	private static List mapClassList;
	
	private final ObjectMapper mapper;
	
	@Autowired
	private DataLogUtil dataLog;

	@Autowired
	public WebControllerAop(ObjectMapper mapper) {
		this.mapper = mapper;
	}
	
	@Pointcut("execution(* com.juzhencms.apps.busi.web..*.*(..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)")
	public void webLog() {
	}
	
	/*@Around("execution(* com.juzhencms.apps.busi.web..*.*(..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)")
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		if(true){//你的校验成功执行方法,失败方法就不用执行了
		     return pjp.proceed();
		}else{
		     return null;
		}
	}*/
	
	@AfterReturning(returning = "response", pointcut = "webLog()")
	public void doAfterReturning(Object response) throws Throwable {
		//处理返回数据，如果由id字段，设置为_id，以便兼容ios版本
		//accessDataField(response);
		//将返回的结果写入日志
		dataLog.setResponse(response);
		
	}
	
	//处理返回数据，如果由id字段，设置为_id，以便兼容ios版本
	private void accessDataField(Object data) {
		if (listClassList == null) {
			listClassList = new ArrayList();
			listClassList.add("java.util.ArrayList");
		}

		if (mapClassList == null) {
			mapClassList = new ArrayList();
			mapClassList.add("java.util.HashMap");
			mapClassList.add("org.springframework.util.LinkedCaseInsensitiveMap");
		}

		if (data == null) {
			return;
		} else if (mapClassList.contains(data.getClass().getName())) {
			Map mapData = (Map) data;

			if (mapData.get("id") != null) {
				mapData.put("_id", mapData.get("id"));
			}

			Set<String> keySet = mapData.keySet(); // 有了Set集合。就可以获取其迭代器。
			Iterator<String> it = keySet.iterator();
			while (it.hasNext()) {
				String key = it.next(); // 有了键可以通过map集合的get方法获取其对应的值。
				Object obj = mapData.get(key);
				if (obj == null)
					continue;
				if (mapClassList.contains(obj.getClass().getName())
						|| listClassList.contains(obj.getClass().getName())) {
					accessDataField(obj);
				}

			}
		} else if (listClassList.contains(data.getClass().getName())) {
			List listData = (List) data;
			for (int i = 0; i < listData.size(); i++) {
				Object obj = listData.get(i);
				if (obj == null)
					continue;
				if (mapClassList.contains(obj.getClass().getName())
						|| listClassList.contains(obj.getClass().getName())) {
					accessDataField(obj);
				}
			}
		}

	}
}
